﻿using Dapper;
using MediatR;
using MySqlX.XDevAPI.Common;
using System.Text;
using Warehouse.Api.Read.AGCation.Command;
using Warehouse.Api.Read.Dtos;
using Warehouse.Domian.Model;
using Warehouse.ErrorCode;
using Warehouse.Infrastructrue.Implenment;

namespace Warehouse.Api.Read.AGCation.Hanlder
{
    public class OutboundCommandHanlder : IRequestHandler<OutboundCommand, ApiResult<List<OutboundDto>>>
    {
        DbContext db;

        public OutboundCommandHanlder(DbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 出库记录查询列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public async Task<ApiResult<List<OutboundDto>>> Handle(OutboundCommand request, CancellationToken cancellationToken)
        {
             var sqlbuilder = new StringBuilder();
            ApiResult<List<OutboundDto>> result = new ApiResult<List<OutboundDto>>();
            try
            {
                
                sqlbuilder.Append(@" SELECT 
                     a.AssociatedOrderNumber,
                     a.PreparedName,
                     a.PreparedTime,
                     a.Price,
                     a.IncomingQuantity,
                     b.DepotName, 
                     c.ProductName 
                     FROM stocking a  
                        JOIN DepotModel b ON a.StorageId = b.DepotId
                      JOIN ProductModel c ON a.ProductId = c.ProductId WHERE a.IsDeleted =1");
                      var list = await db.DapperConnection.QueryAsync<OutboundDto>(sqlbuilder.ToString());
                result.Data = list.ToList();
                result.Code = ApiEume.Success;
                  result.Msg = "出库记录查询成功";
               


            }
            catch (Exception ex)
            {

            }
            return result;
        }
    }
}
